home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / HD-Installer / jst_dev / sources / src / DiskTools / grem2file.c < prev    next >
C/C++ Source or Header  |  2000-04-12  |  4KB  |  185 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4.  
  5. #include <dos/dos.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <exec/execbase.h>
  9. #include <exec/libraries.h>
  10. #include <exec/memory.h>
  11. #include <pragmas/dos_pragmas.h>
  12. #include <string.h>
  13.  
  14. #define RAWTRACK_LEN 0x8000
  15. #define TRACK_LEN 0x1800
  16. #define NB_RETRIES 5
  17.  
  18. extern struct ExecBase *SysBase;
  19.  
  20. extern APTR InitTrackDisk(ULONG);
  21. extern void ShutTrackDisk(void);
  22. extern ULONG CheckDiskIn (void);
  23.  
  24. extern ULONG ReadRawTrackIndex(ULONG,UBYTE *);
  25. extern ULONG DecodeTrack(UBYTE *, UBYTE *);
  26. UBYTE *rawTrackBuffer=0L;
  27. UBYTE *decTrackBuffer=0L;
  28. char *charunit="DF0:";
  29. int allocated=0,diskinit=0;
  30. FILE *f;
  31.  
  32. void CloseAll(char *mess)
  33.  
  34. {
  35. if (f) fclose(f);
  36.  
  37. if (mess) printf("** %s\n",mess);
  38.  
  39. if (diskinit) ShutTrackDisk();
  40. if (allocated) Inhibit(charunit,FALSE);
  41. if (rawTrackBuffer) FreeMem((APTR)rawTrackBuffer,RAWTRACK_LEN);
  42. if (decTrackBuffer) FreeMem((APTR)decTrackBuffer,TRACK_LEN);
  43.  
  44.  
  45. exit(0);
  46. }
  47.  
  48. void BreakHandle(int code)
  49.  
  50. {
  51. CloseAll("User break");
  52. }
  53.  
  54.  
  55. int findArg(unsigned int argc,char ** argv,char * argstr)
  56. {
  57.   int argl,i;
  58.  
  59.   argl=strlen(argstr);
  60.  
  61.   for (i=0;i<argc;i++)
  62.     if (!strncmp(argv[i],argstr,argl)) return i;
  63.  
  64.   return 0;
  65. }
  66.  
  67.  
  68. main(unsigned int argc,char ** argv)
  69.  
  70.  
  71. {
  72.   ULONG diskunit=0,starttrack=2,endtrack=159; /* default values */
  73.   ULONG ignore_errors=0;
  74.   int i,retries,readok,dcrslt;
  75.   char filename[108]="";
  76.  
  77.   signal(SIGINT,BreakHandle); /* CTRL-C caught */
  78.  
  79.   printf("\2331;37;40mGrem2File V1.2a \2330;31;40m - The Gremlins Disks image maker by JF Fabre\n");
  80.  
  81.   switch(argc)
  82.  
  83.      {
  84.  
  85. /*    case 6:
  86.         if (!stricmp(argv[5],"IGNERR")) ignore_errors=1;*/
  87.     case 5:
  88.         endtrack=atoi(argv[4]);
  89.         if ((endtrack>159)||(endtrack<0))
  90.               CloseAll("StartTrack must be between 0 and 159\n");
  91.     case 4:
  92.         starttrack=atoi(argv[3]);
  93.         if ((starttrack>endtrack)||(starttrack<0))
  94.               CloseAll("StartTrack must be between 0 and 159\n");
  95.  
  96.     case 3:
  97.         strcpy(filename,argv[2]);
  98.         diskunit=(ULONG)argv[1][0]-'0';
  99.         break;
  100.         
  101.     default:
  102.         printf("\nUsage : grem2file <unit> <filename> [<starttrack>] [<endtrack>]\n");
  103.         printf("Defaults are:\n  StartTrack: 2\n  EndTrack: 159\n\nYou must specify a filename for the destination\n");
  104.         if (!argc) Delay(200);
  105.         CloseAll(0);
  106.     }
  107.  
  108.  
  109.   diskunit=(ULONG)argv[1][0]-'0';
  110.  
  111.   if ((diskunit>3)||(diskunit<0)) {printf("** Unit %d unavailable\n",diskunit);CloseAll(0);}
  112.  
  113.   charunit[2]=diskunit+'0';
  114.  
  115.   if (SysBase->LibNode.lib_Version>36)
  116.     {
  117.     if(Inhibit(charunit,DOSTRUE)==FALSE) CloseAll("Can't allocate device !");
  118.     allocated=1;
  119.     }
  120.  
  121.   if ((ULONG)InitTrackDisk(diskunit)<0) {printf("** Can't open unit %d !",diskunit);CloseAll(0);}
  122.   diskinit=1;
  123.   if (CheckDiskIn()) {printf("** No disk in unit %d !\n",diskunit);CloseAll(NULL);}
  124.  
  125.   decTrackBuffer=(char *)AllocMem(TRACK_LEN,MEMF_CLEAR);
  126.   if (decTrackBuffer==0L) CloseAll("Can't allocate memory.");
  127.  
  128.   rawTrackBuffer=(char *)AllocMem(RAWTRACK_LEN,MEMF_CHIP|MEMF_CLEAR);
  129.   if (rawTrackBuffer==0L) CloseAll("Can't allocate chip memory.");
  130.  
  131.   f=fopen(filename,"wb");
  132.  
  133.   if (f==NULL) CloseAll("Can't create destination file!");
  134.  
  135.   printf("Reading disk from track %d to track %d (included)\n",starttrack,endtrack);
  136.  
  137.   for (i=starttrack;i<endtrack+1;i++)
  138.     {
  139.     retries=0;
  140.     readok=0;
  141.  
  142.     do
  143.     {
  144.  
  145.         if (ReadRawTrackIndex(i^1,rawTrackBuffer))
  146.             {
  147.             if (retries>NB_RETRIES) CloseAll("Low level disk read error!");
  148.             }
  149.  
  150.         else
  151.         
  152.             {    
  153.  
  154.              dcrslt=DecodeTrack(rawTrackBuffer,decTrackBuffer);
  155.  
  156.             if (!dcrslt)
  157.                     {
  158.                         readok=1;
  159.                         if (fwrite(decTrackBuffer,1,TRACK_LEN,f)!=TRACK_LEN) CloseAll("Disk full !");
  160.                     }
  161.  
  162.             else
  163.                     {
  164.                         if (retries>NB_RETRIES)
  165.  
  166.                             switch(dcrslt)
  167.                             {    
  168.                             case -1: printf("** Error Track %d\n",i);CloseAll("Decode disk read error!");
  169.                             default: printf("** Error Track %d\n",i);CloseAll("No sync found!");
  170.                             }
  171.                     }
  172.  
  173.             }
  174.  
  175.     retries++;
  176.     }
  177.     while (!readok);
  178.     }
  179.  
  180.     printf("Disk image done\n");
  181.  
  182.     CloseAll(0);
  183. }
  184.  
  185.